package com.iris.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCTemplate {

	private final static String properties = "com/iris/utils/jdbc.properties";
//	private final static String properties = "jdbc.properties";
	public static Properties pro;
	private DataDict.DataType dbType=DataDict.DataType.MYSQL;
	public JDBCTemplate(){
		initProperties();
	}
	public JDBCTemplate(DataDict.DataType dbType){
		this.dbType=dbType;
		initProperties();
	}
	public Connection getConnect(){
		Connection conn=null;
		try {
			Class.forName(pro.getProperty(DataDict.JdbcKey.DRIVERCLASS.getValue()+"."+dbType.getValue()));
			conn = DriverManager.getConnection(
					pro.getProperty(DataDict.JdbcKey.URL.getValue()+"."+dbType.getValue()), 
					pro.getProperty(DataDict.JdbcKey.USERNAME.getValue()+"."+dbType.getValue()), 
					pro.getProperty(DataDict.JdbcKey.PASSWORD.getValue()+"."+dbType.getValue()));
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return conn;
	}

	private static void initProperties() {
		InputStream in = null;
		pro = new Properties();
		try {
			in = JDBCTemplate.class.getClassLoader()
					.getResourceAsStream(properties);
			System.out.println(JDBCTemplate.class.getClass().getResource("/").getPath());
			pro.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static Properties getPro()  {
		if( pro == null){
			initProperties();
		}
			return pro;
	}
}
